//
//  DengLuViewController.m
//  TableViewGroup
//
//  Created by 侯振兴 on 15/1/6.
//  Copyright (c) 2015年 侯振兴. All rights reserved.
//

#import "DengLuViewController.h"
#import <ShareSDK/ShareSDK.h>
#import "HomePageViewController.h"
#import "MSMenuView.h"
#import "ReadingRoomViewController.h"
#import "SettingsViewController.h"
#import "Masonry.h"
#import "WebColor.h"
#import "DetailBace.h"
#import "DetailModel.h"

@interface DengLuViewController ()

@end

@implementation DengLuViewController

- (void)dealloc
{
    [_iam release];
    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
    }
    return self;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.iam = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
    _iam.center = CGPointMake(self.view.frame.size.width / 2, self.view.frame.size.height / 2);
    _iam.image = [UIImage imageNamed:@"tiankonglan1.png"];
    [self.view addSubview:_iam];
    [_iam release];
    [self createDeng];
    [self animationA];
}


- (void)createDeng
{
//    UIImageView *imageView = [[UIImageView alloc] initWithFrame:self.view.frame];
//    imageView.image = [UIImage imageNamed:@"haiyang1"];
//    [self.view addSubview:imageView];
    
    UIButton *xinLangButton = [[UIButton alloc] init];
    [xinLangButton addTarget:self action:@selector(xinLangAction:) forControlEvents:UIControlEventTouchUpInside];
    [xinLangButton setBackgroundImage:[UIImage imageNamed:@"weibo icon.png"] forState:UIControlStateNormal];
    [self.view addSubview:xinLangButton];
    [xinLangButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left).offset(self.view.frame.size.width / 5 + self.view.frame.size.width / 5);
        make.top.equalTo(self.view.mas_top).offset(self.view.frame.size.height / 2);
        make.size.mas_equalTo(CGSizeMake(self.view.frame.size.width / 5, self.view.frame.size.width / 5));
    }];

    
    UIButton *youkeButton = [[UIButton alloc] initWithFrame:CGRectMake(self.view.frame.size.width / 2 - 50, self.view.frame.size.height - 180, 100, 30)];
    [youkeButton setTitle:@"返回" forState:UIControlStateNormal];
    [youkeButton setBackgroundImage:[UIImage imageNamed:@"Button3.png"] forState:UIControlStateNormal];
    [youkeButton addTarget:self action:@selector(youkeAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:youkeButton];
    

    [xinLangButton release];
    [youkeButton release];
}


- (void)createImage
{
    self.view.frame = [[UIScreen mainScreen] bounds];
    NSInteger num = self.view.frame.size.height;
    for (int i = 0; i < 4; i++) {
        NSInteger startX = arc4random()%100 + 60;
        NSInteger startY = arc4random()%num;
        NSInteger lastY = arc4random()%num;
        
        NSInteger value = arc4random()%5 + 10;
        CGFloat bValue = value/10.0f;
        UIImageView *imag = [[UIImageView alloc] initWithFrame:CGRectMake(-startX, startY, 40 * bValue, 40 * bValue)];
        NSString *pathName = [NSString stringWithFormat:@"pugogying.png"];
        imag.image = [UIImage imageNamed:pathName];
        [_iam addSubview:imag];
        [imag release];
        [UIView animateWithDuration:5 animations:^{
            imag.frame = CGRectMake(self.view.frame.size.width, lastY, 40 * bValue, 40 * bValue);
        } completion:^(BOOL finished) {
            [imag removeFromSuperview];
        }];
    }
    [NSTimer scheduledTimerWithTimeInterval:0.30f target:self selector:@selector(createImage) userInfo:nil repeats:NO];
}

     
     
- (void)xinLangAction:(id)sender
{
    [ShareSDK getUserInfoWithType:ShareTypeSinaWeibo authOptions:nil result:^(BOOL result, id<ISSPlatformUser> userInfo, id<ICMErrorInfo> error) {
        //        NSLog(@"%d",result);
        if (result) {
#warning 用户信息
          NSUserDefaults *fault = [NSUserDefaults standardUserDefaults];
            [fault setObject:[userInfo nickname] forKey:@"name"];
            [fault setObject:[userInfo profileImage] forKey:@"image"];
            [fault synchronize];
                        self.uid = [userInfo uid];
            NSInteger nid = [_uid integerValue];
            [fault setObject:_uid forKey:@"nid"];
            DetailModel *model = [DetailModel detailModelWithDetail_id:nid detail_tit:@"" detail_http:@""];
            [DetailBace insertIntoDetailWithDetailModel:model];
                        self.nickname = [userInfo nickname];
                        self.profileImage = [userInfo profileImage];
                        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"登陆成功" message:[NSString stringWithFormat:@"授权登陆成功, 用户ID:%@, 昵称:%@,头像:%@", _uid, _nickname, _profileImage] delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
                        [alert show];
            
                        SettingsViewController *homePageController = [[[SettingsViewController alloc] init] autorelease];
                        [self.navigationController pushViewController:homePageController animated:YES];
            
                        [alert release];
            
            
            
            //存入登陆状态
            NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
            [user setObject:@"yes" forKey:@"login"];
            [[NSNotificationCenter defaultCenter] postNotificationName:@"denglu" object:self userInfo:nil];
            
            }else{
        }
    }];
         
}


- (void)youkeAction:(id)sender
{
    [self dismissViewControllerAnimated:NO completion:nil];
    [self.navigationController popViewControllerAnimated:NO];
}


#warning 360旋转
- (void)animationA
{
    
    [UIView animateWithDuration:0 animations:^{
        _iam.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
    } completion:^(BOOL finished) {
        [self createImage];
    }];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
